#include <windows.h>
#include <tchar.h>
#include <stdio.h>

#include "afxwin.h"


HINSTANCE AfxGetInstanceHandle()
{
	return ::GetModuleHandle( NULL );
}

int AfxMessageBox( LPCTSTR lpszText, UINT nType, UINT nIDHelp )
{
	if ( nIDHelp != 0 ) {
		nType |= MB_HELP;
	}

	return ::MessageBox(NULL, lpszText, NULL, nType );
}

CWinApp* AfxGetApp()
{
	fprintf(stderr, "debug: 0x%08X = AfxGetApp()\n", afxCurrentWinApp);

	return afxCurrentWinApp;
}

CWinThread* AfxGetThread() {
	static CWinThread *thread = new CWinThread();

	return thread;
}

LRESULT CALLBACK AfxWndProc(HWND hWnd, UINT message, UINT wParam, LONG lParam)
{
	HANDLE h = ::GetProp( hWnd, "MyClassPointer" );
	CWnd* wnd = reinterpret_cast<CWnd *>(h);

	// ...
	if ( wnd ) {
		wnd->WindowProc(message, wParam, lParam);
	}

	// ...
	if  (message == WM_DESTROY )
	{
		PostQuitMessage(0);
		return 0;
	}

	return DefWindowProc(hWnd,message,wParam,lParam);
}

LPCTSTR AfxRegisterWndClass(UINT nClassStyle, HCURSOR hCursor, HBRUSH hbrBackground, HICON hIcon)
{
	static TCHAR szClassName[128];
	static int nId = 0;
	WNDCLASS wc;

	// ...
	wsprintf( szClassName, "OpenMFC.WndClass.%d", ++nId );

	fprintf(stderr, "debug: %s = AfxRegisterWndClass()\n", szClassName );

	// ...
	wc.style = nClassStyle | CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = AfxWndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = AfxGetInstanceHandle();
	wc.hIcon = ::LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
	wc.lpszMenuName = NULL;
	wc.lpszClassName = szClassName;

	// ...
	if ( !RegisterClass(&wc) ) {
		return NULL;
	}

	// ...
	return (LPCTSTR)&szClassName;
}